package cn.woochen.mvvmframe.helper

import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

/**
 *类描述：通知帮助类
 *@author woochen
 *@time 2021/9/8 16:45
 */
object NotificationHelper {

    /**
     * 注册通知渠道(对应通知设置中的通知类型)
     *
     * @param context
     * @param channelId
     * @param channelName 渠道名称
     * @param channelDesc 渠道描述
     * @param importance 通知的重要性(默认为高，有声音并显示在通知栏)
     */
    fun createNotificationChannel(context: Context, channelId: String, channelName: String, channelDesc: String, importance: Int = NotificationManager.IMPORTANCE_HIGH) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, channelName, importance).apply {
                description = channelDesc
            }
            val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }


    /**
     * 发送通知
     * @param context
     * @param channelId 渠道id
     * @param notifyId 通知id，之后用于取消通知等操作(需要保证唯一性)
     * @param resIcon 通知小图标
     * @param title 通知标题
     * @param subtitle 通知副标题
     * @param targetCls 点击通知时跳转的Activity
     * @return 通知id
     */
    fun sendNotify(context: Context, channelId: String, notifyId: Int, resIcon: Int, title: String, subtitle: String, targetCls: Class<*>): Int {
        val intent = Intent(context, targetCls).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(resIcon)
            .setContentTitle(title)
            .setContentText(subtitle)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
        with(NotificationManagerCompat.from(context)) {
            notify(notifyId, builder.build())
        }
        return notifyId
    }


    /* fun sendMsgNotify(context: Context,channelId:String,notifyId:Int,resIcon:Int,title:String,subtitle:String,targetCls:Class<*>){
         val intent = Intent(context, targetCls).apply {
             flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
         }
         val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
         val person = Person.Builder().setName("person1").setKey("1").build()
         val builder = NotificationCompat.Builder(context, channelId)
             .setSmallIcon(resIcon)
             .setContentTitle(title)
             .setContentText(subtitle)
             .setDefaults(Notification.DEFAULT_SOUND)
             .setPriority(NotificationCompat.PRIORITY_HIGH)
             .setStyle(NotificationCompat.MessagingStyle(person)
                 .addMessage(NotificationCompat.MessagingStyle.Message("消息正文1",
                     System.currentTimeMillis(),person))
                 .addMessage(NotificationCompat.MessagingStyle.Message("消息正文2",
                     System.currentTimeMillis(),person))
             )
             .setContentIntent(pendingIntent)
             .setAutoCancel(true)
         with(NotificationManagerCompat.from(context)){
             notify(notifyId,builder.build())
         }
     }*/
}